Expand description
A 100% CommonMark and GFM compatible Markdown parser. Source repository is at https://github.com/kivikakk/comrak.
The design is based on cmark, so familiarity with that will help.
You can use comrak::markdown_to_html
directly:
use comrak::{markdown_to_html, ComrakOptions};
assert_eq!(markdown_to_html("Hello, **世界**!", &ComrakOptions::default()),
"<p>Hello, <strong>世界</strong>!</p>\n");
Or you can parse the input into an AST yourself, manipulate it, and then use your desired formatter:
use comrak::{Arena, parse_document, format_html, ComrakOptions};
use comrak::nodes::{AstNode, NodeValue};
// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
let arena = Arena::new();
let root = parse_document(
&arena,
"This is my input.\n\n1. Also my input.\n2. Certainly my input.\n",
&ComrakOptions::default());
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
where F : Fn(&'a AstNode<'a>) {
f(node);
for c in node.children() {
iter_nodes(c, f);
}
}
iter_nodes(root, &|node| {
match &mut node.data.borrow_mut().value {
&mut NodeValue::Text(ref mut text) => {
let orig = std::mem::replace(text, String::new());
*text = orig.replace("my", "your");
}
_ => (),
}
});
let mut html = vec![];
format_html(root, &ComrakOptions::default(), &mut html).unwrap();
assert_eq!(
String::from_utf8(html).unwrap(),
"<p>This is your input.</p>\n\
<ol>\n\
<li>Also your input.</li>\n\
<li>Certainly your input.</li>\n\
</ol>\n");
Re-exports
pub use html::format_document as format_html;
pub use html::format_document_with_plugins as format_html_with_plugins;
pub use html::Anchorizer;
Modules
- Adapter traits for plugins.
- Included from https://github.com/SimonSapin/rust-forest/blob/5783c8be8680b84c0438638bdee07d4e4aca40ac/arena-tree/lib.rs. MIT license (per Cargo.toml).
- The HTML renderer for the CommonMark AST, as well as helper functions.
- The CommonMark AST.
- Plugins for enhancing the default implementation of comrak can be defined in this module.
Structs
- An arena of objects of type
T
. - Options to select extensions.
- Umbrella options struct.
- Options for parser functions.
- Umbrella plugins struct.
- Options for formatter functions.
- Plugins for alternative rendering.
Enums
- Options for bulleted list redering in markdown. See
link_style
in ComrakRenderOptions for more details.
Functions
- Formats an AST as CommonMark, modified by the given options.
- Formats an AST as CommonMark, modified by the given options. Accepts custom plugins.
- Formats an AST as HTML, modified by the given options.
- Formats an AST as HTML, modified by the given options. Accepts custom plugins.
- Render Markdown back to CommonMark.
- Render Markdown to CommonMark XML. See https://github.com/commonmark/commonmark-spec/blob/master/CommonMark.dtd.
- Render Markdown to CommonMark XML using plugins. See https://github.com/commonmark/commonmark-spec/blob/master/CommonMark.dtd.
- Render Markdown to HTML.
- Render Markdown to HTML using plugins.
- Parse a Markdown document to an AST.
- Parse a Markdown document to an AST.
- Return the version of the crate.